using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using Random = UnityEngine.Random;

public static class ZTool
{
    #region Common

    /// <summary>复制文本到剪切板</summary>
    public static void CopyText(string text) => GUIUtility.systemCopyBuffer = $"{text}";

    #endregion

    #region IO

    public static string CheckAndCreateDirectory(this string path)
    {
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }

        return path;
    }

    #endregion

    #region Transform

    /// <summary>把世界坐标转成屏幕坐标</summary>
    public static Vector2 WorldToScreenPoint(Canvas canvas, Vector3 world)
    {
        Vector2 screenPoint = RectTransformUtility.WorldToScreenPoint(canvas.worldCamera, world);
        Vector2 localPoint;
        //把屏幕坐标转成局部坐标
        RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.GetComponent<RectTransform>(), screenPoint, canvas.worldCamera,
            out localPoint);

        return localPoint;
    }

    public static void Identity(this Transform transform)
    {
        transform.localPosition = Vector3.zero;
        transform.localRotation = Quaternion.identity;
        transform.localScale = Vector3.one;
    }

    public static void SetLocalPosX(this Transform transform, float x) =>
        transform.localPosition = new Vector3(x, transform.localPosition.y, transform.localPosition.z);

    public static void SetLocalPosY(this Transform transform, float y) =>
        transform.localPosition = new Vector3(transform.localPosition.x, y, transform.localPosition.z);

    public static void SetLocalPosZ(this Transform transform, float z) =>
        transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, z);

    public static void SetLocalPosXY(this Transform transform, float x, float y) =>
        transform.localPosition = new Vector3(x, y, transform.localPosition.z);

    public static void SetLocalPosXZ(this Transform transform, float x, float z) =>
        transform.localPosition = new Vector3(x, transform.localPosition.y, z);

    public static void SetLocalPosYZ(this Transform transform, float y, float z) =>
        transform.localPosition = new Vector3(transform.localPosition.x, y, z);

    public static void AddChild(this Transform transform, Transform child) => child.SetParent(transform);

    #endregion

    #region Math

    public static bool IsInRange(int range, int minInclusive = 0, int maxExclusive = 100) =>
        Random.Range(minInclusive, maxExclusive) < range;

    public static T GetRandomFrom<T>(params T[] values) => values[Random.Range(0, values.Length)];

    #endregion

    #region Others

    public static bool InTimeRange(int year, int month, int day, int hour, int minute = 0, int second = 0) =>
        DateTime.Now < new DateTime(year, month, day, hour, minute, second);

    #endregion
}